{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n    @Author: King\\n    @Date: 2019.06.01\\n    @Purpose: DGL at a Glance\\n    @Introduction:   DGL is a Python package dedicated to deep learning on graphs, built atop existing tensor DL frameworks (e.g. Pytorch, MXNet) and simplifying the implementation of graph-based neural networks.\\n    @Datasets: \\n    @Link : \\n    @Reference : https://docs.dgl.ai/tutorials/basics/1_first.html\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "    @Author: King\n",
    "    @Date: 2019.06.01\n",
    "    @Purpose: DGL at a Glance\n",
    "    @Introduction:   DGL is a Python package dedicated to deep learning on graphs, built atop existing tensor DL frameworks (e.g. Pytorch, MXNet) and simplifying the implementation of graph-based neural networks.\n",
    "    @Datasets: \n",
    "    @Link : \n",
    "    @Reference : https://docs.dgl.ai/tutorials/basics/1_first.html\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step 0: Problem description\n",
    "\n",
    "We start with the well-known “Zachary’s karate club” problem. The karate club is a social network which captures 34 members and document pairwise links between members who interact outside the club. The club later divides into two communities led by the instructor (node 0) and the club president (node 33). The network is visualized as follows with the color indicating the community:\n",
    "\n",
    "![karate-clu](img/karate-club.png)\n",
    "\n",
    "The task is to predict which side (0 or 33) each member tends to join given the social network itself.\n",
    "该任务是用于预测每个成员趋向于加入给定社交网络中的哪一方（0 or 33）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step 1: Creating a graph in DGL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We have 34 nodes.\n",
      "We have 156 edges.\n"
     ]
    }
   ],
   "source": [
    "# Creating the graph for Zachary’s karate club goes as follows:\n",
    "import dgl\n",
    "\n",
    "def build_karate_club_graph():\n",
    "    g = dgl.DGLGraph()\n",
    "    # add 34 nodes into the graph; nodes are labeled from 0~33\n",
    "    g.add_nodes(34)\n",
    "    # all 78 edges as a list of tuples\n",
    "    edge_list = [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2),\n",
    "        (4, 0), (5, 0), (6, 0), (6, 4), (6, 5), (7, 0), (7, 1),\n",
    "        (7, 2), (7, 3), (8, 0), (8, 2), (9, 2), (10, 0), (10, 4),\n",
    "        (10, 5), (11, 0), (12, 0), (12, 3), (13, 0), (13, 1), (13, 2),\n",
    "        (13, 3), (16, 5), (16, 6), (17, 0), (17, 1), (19, 0), (19, 1),\n",
    "        (21, 0), (21, 1), (25, 23), (25, 24), (27, 2), (27, 23),\n",
    "        (27, 24), (28, 2), (29, 23), (29, 26), (30, 1), (30, 8),\n",
    "        (31, 0), (31, 24), (31, 25), (31, 28), (32, 2), (32, 8),\n",
    "        (32, 14), (32, 15), (32, 18), (32, 20), (32, 22), (32, 23),\n",
    "        (32, 29), (32, 30), (32, 31), (33, 8), (33, 9), (33, 13),\n",
    "        (33, 14), (33, 15), (33, 18), (33, 19), (33, 20), (33, 22),\n",
    "        (33, 23), (33, 26), (33, 27), (33, 28), (33, 29), (33, 30),\n",
    "        (33, 31), (33, 32)]\n",
    "    # add edges two lists of nodes: src and dst\n",
    "    src, dst = tuple(zip(*edge_list))\n",
    "    g.add_edges(src, dst)\n",
    "    # edges are directional in DGL; make them bi-directional\n",
    "    g.add_edges(dst, src)\n",
    "\n",
    "    return g\n",
    "\n",
    "\n",
    "# We can print out the number of nodes and edges in our newly constructed graph:\n",
    "G = build_karate_club_graph()\n",
    "print('We have %d nodes.' % G.number_of_nodes())\n",
    "print('We have %d edges.' % G.number_of_edges())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "# Since the actual graph is undirected, we convert it for visualization\n",
    "# purpose.\n",
    "nx_G = G.to_networkx().to_undirected()\n",
    "# Kamada-Kawaii layout usually looks pretty for arbitrary graphs\n",
    "pos = nx.kamada_kawai_layout(nx_G)\n",
    "nx.draw(nx_G, pos, with_labels=True, node_color=[[.7, .7, .7]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
